package ${domainName}${projectName}${moduleName}.controller;

import ${domainName}${projectName}${moduleName}.domain.${entity}ListVO;
import ${domainName}${projectName}${moduleName}.domain.${entity}VO;
import com.dishdex.menuhub.module.entity.${entity};
import com.dishdex.menuhub.module.service.I${entity}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ${entity}Controller{
        @Autowired
        private I${entity}Service ${entity?uncap_first}Service;

        @GetMapping("/${entity?uncap_first}/list")
        public ${entity}ListVO ${entity?uncap_first}List(){
                List<${entity}> l1;
                try{
                        l1=${entity?uncap_first}Service.getAll${entity}();
                }catch(RuntimeException e){
                        ${entity}ListVO vo = new ${entity}ListVO();
                        vo.setMessage(e.getMessage());
                        return vo;
                }
                List<${entity}VO> l2 = new ArrayList<>(l1.size());
                for(${entity} x:l1){
                        l2.add(convertTo${entity}VO(x));
                }
                ${entity}ListVO vo = new ${entity}ListVO();
                vo.setList(l2);
                return vo;
}

        private ${entity}VO convertTo${entity}VO(${entity} x){
        ${entity}VO vo = new ${entity}VO();
        <#list table.fields as field>
                <#if field.propertyName!="createTime"&&field.propertyName!="updateTime"&&field.propertyName!="isDeleted">
                        <#if field.propertyType=="String">
                                vo.set${field.propertyName?cap_first}(x.get${field.propertyName?cap_first}()==null?"":x.get${field.propertyName?cap_first}());
                        <#else>
                                vo.set${field.propertyName?cap_first}(x.get${field.propertyName?cap_first}());
                        </#if>
                </#if>
        </#list>
        return vo;
        }

        @GetMapping("/${entity?uncap_first}/info")
        public ${entity}VO ${entity?uncap_first}Info(@RequestParam(value="id",required=false) Long id){
                ${entity} x;
                try{
                        x=${entity?uncap_first}Service.get${entity}ById(id);
                }catch(RuntimeException e){
                        ${entity}VO vo = new ${entity}VO();
                        vo.setMessage(e.getMessage());
                        return vo;
        }
                ${entity}VO vo = new ${entity}VO();
        <#list table.fields as field>
                <#if field.propertyName!="isDeleted"&&field.propertyName!="createTime"&&field.propertyName!="updateTime">
                        <#if field.propertyType=="String">
                                vo.set${field.propertyName?cap_first}(x.get${field.propertyName?cap_first}()==null?"":x.get${field.propertyName?cap_first}());
                        <#else>
                                vo.set${field.propertyName?cap_first}(x.get${field.propertyName?cap_first}());
                        </#if>
                </#if>
        </#list>
                                vo.setCreateTime(x.getCreateTime()==null?"":String.valueOf(x.getCreateTime()));
                                vo.setUpdateTime(x.getUpdateTime()==null?"":String.valueOf(x.getUpdateTime()));
        return vo;
        }

        @GetMapping("/${entity?uncap_first}/update")
        public ${entity}VO ${entity?uncap_first}Update(
        <#assign filteredFields = table.fields?filter(f ->
        f.columnName != "create_time"
        && f.columnName != "update_time"
        && f.columnName != "is_deleted")>
        <#list filteredFields as field>
               @RequestParam(value="${field.propertyName}",required=false) ${field.propertyType} ${field.propertyName}<#sep>,
        </#list>
        ){
        Long result;
        try{
                result = ${entity?uncap_first}Service.update${entity}(
        <#list filteredFields as field>
                ${field.propertyName}<#sep>,
        </#list>
        );
        }catch(RuntimeException e){
                ${entity}VO vo = new ${entity}VO();
                vo.setMessage(e.getMessage());
                return vo;
        }
        ${entity}VO vo = new ${entity}VO();
        vo.setMessage(String.valueOf(result));
        return vo;
        }

        @GetMapping("/${entity?uncap_first}/insert")
        public ${entity}VO ${entity?uncap_first}Insert(
        <#assign filteredFields = table.fields?filter(f ->
        f.columnName != "create_time"
        && f.columnName != "update_time"
        && f.columnName != "is_deleted"
        && f.columnName != "id")>
        <#list filteredFields as field>
                @RequestParam(value="${field.propertyName}",required=false) ${field.propertyType} ${field.propertyName}<#sep>,
        </#list>
        ){
        Long result;
        try{
        result = ${entity?uncap_first}Service.insert${entity}(
        <#list filteredFields as field>
                ${field.propertyName}<#sep>,
        </#list>
        );
        }catch(RuntimeException e){
        ${entity}VO vo = new ${entity}VO();
        vo.setMessage(e.getMessage());
        return vo;
        }
        ${entity}VO vo = new ${entity}VO();
        vo.setMessage(String.valueOf(result));
        return vo;
        }

        @GetMapping("/${entity?uncap_first}/delete")
        public ${entity}VO ${entity?uncap_first}Delete(@RequestParam(value="id",required=false) Long id){
        int result;
        try{
                result=${entity?uncap_first}Service.delete${entity}(id);
        }catch(RuntimeException e){
                ${entity}VO vo = new ${entity}VO();
                vo.setMessage(e.getMessage());
                return vo;
        }
        ${entity}VO vo = new ${entity}VO();
        vo.setMessage(result==1?"成功":"失败");
        return vo;
        }

        }